local yisiquanhuang = fk.CreateSkill{
    name = "rmt__yisiquanhuang",
    tags = {Skill.Compulsory}
}
Fk:loadTranslationTable{
    ["rmt__yisiquanhuang"] = "疑似拳皇",
    [":rmt__yisiquanhuang"] = "锁定技，你的红/黑色手牌分别置于两个独立计算且不能整理的手牌区，失去其中一个区域内的所有牌后，将弃牌堆随机两张符合另一区域的牌置入之。",
    ["@[rmt__yisiquanhuang]"] = "疑似拳皇",
    ["@@rmt__yisiquanhuang_left-inhand"] = "最左",
    ["@@rmt__yisiquanhuang_right-inhand"] = "最右",
}

Fk:addQmlMark{
    name = "rmt__yisiquanhuang",
    qml_path = function(name, value, p)
        if Self == p then
            return "packages/aaa_Romantic/qml/rmt__yisiquanhuang"
        end
        return ""
    end,
    how_to_show = function(name, value, p)
        return " "
    end,
}

local spec = {
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        local handcards = player:getCardIds("h")
        local redCards = table.filter(handcards,function (id)
            local card = Fk:getCardById(id)
            return card.color == Card.Red
        end)
        local blackCards = table.filter(handcards,function (id)
            local card = Fk:getCardById(id)
            return card.color == Card.Black
        end)

        for index, id in ipairs(redCards) do
            local card = Fk:getCardById(id)
            if index == 1 then
            room:setCardMark(card, "@@rmt__yisiquanhuang_left-inhand", 1)
            end
            if index == #redCards then
            room:setCardMark(card, "@@rmt__yisiquanhuang_right-inhand", 1)
            end
    
            if card:getMark("@@rmt__yisiquanhuang_left-inhand") ~= 0 and index > 1 then
            room:setCardMark(card, "@@rmt__yisiquanhuang_left-inhand", 0)
            end
            if card:getMark("@@rmt__yisiquanhuang_right-inhand") ~= 0 and index < #redCards then
            room:setCardMark(card, "@@rmt__yisiquanhuang_right-inhand", 0)
            end
        end
        for index, id in ipairs(blackCards) do
            local card = Fk:getCardById(id)
            if index == 1 then
            room:setCardMark(card, "@@rmt__yisiquanhuang_left-inhand", 1)
            end
            if index == #blackCards then
            room:setCardMark(card, "@@rmt__yisiquanhuang_right-inhand", 1)
            end
    
            if card:getMark("@@rmt__yisiquanhuang_left-inhand") ~= 0 and index > 1 then
            room:setCardMark(card, "@@rmt__yisiquanhuang_left-inhand", 0)
            end
            if card:getMark("@@rmt__yisiquanhuang_right-inhand") ~= 0 and index < #blackCards then
            room:setCardMark(card, "@@rmt__yisiquanhuang_right-inhand", 0)
            end
        end
    
        if player:getMark("@[rmt__yisiquanhuang]") == 0 then
            room:setPlayerMark(player, "@[rmt__yisiquanhuang]", { value = player.id })
        end
        room:setPlayerMark(player, MarkEnum.SortProhibited, 1)
    end,
}

yisiquanhuang:addEffect(fk.AfterCardsMove, {
    anim_type = "drawcard",
    can_trigger = function (self, event, target, player, data)
        if player:hasSkill(yisiquanhuang.name) then
            for _, move in ipairs(data) do
                if move.from == player then
                    if move.extra_data then
                        if move.extra_data.rmt__yisiquanhuangRed and not table.find(player:getCardIds("h"), function(id) return Fk:getCardById(id).color == Card.Red end) then
                            return true
                        end
                        if move.extra_data.rmt__yisiquanhuangBlack and not table.find(player:getCardIds("h"), function(id) return Fk:getCardById(id).color == Card.Black end) then
                            return true
                        end
                    end
                end
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local colors = {}
        for _, move in ipairs(data) do
            if move.from == player then
                if move.extra_data then
                    if move.extra_data.rmt__yisiquanhuangRed and not table.find(player:getCardIds("h"), function(id) return Fk:getCardById(id).color == Card.Red end) then
                        table.insertIfNeed(colors,"spade,club")
                    end
                    if move.extra_data.rmt__yisiquanhuangBlack and not table.find(player:getCardIds("h"), function(id) return Fk:getCardById(id).color == Card.Black end) then
                        table.insertIfNeed(colors,"heart,diamond")
                    end
                    if #colors > 1 then
                        break
                    end
                end
            end
        end
        for _, color in ipairs(colors) do
            local cards = room:getCardsFromPileByRule(".|.|"..color, 2, "discardPile")
            if #cards then
                room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, yisiquanhuang.name)
            end
        end
    end,

    can_refresh = function (self, event, target, player, data)
        if not player:hasSkill(yisiquanhuang.name, true) or player:isKongcheng() then
            return false
        end
        return table.find(data,function(move)
            if move.to == player and move.toArea == Card.PlayerHand then
                return true
            end
            if move.from == player and table.find(move.moveInfo, function(info) return info.fromArea == Card.PlayerHand end) then
                return true
            end
            return false
        end)
    end,
    on_refresh = spec.on_refresh,
})

yisiquanhuang:addEffect(fk.PreCardUse, {
    can_refresh = function (self, event, target, player, data)
        if not player:hasSkill(yisiquanhuang.name, true) or player:isKongcheng() then
            return false
        end
        return target == player
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        local handcards = player:getCardIds("h")
        local redCards = table.filter(handcards,function (id)
            local card = Fk:getCardById(id)
            return card.color == Card.Red
        end)
        local blackCards = table.filter(handcards,function (id)
            local card = Fk:getCardById(id)
            return card.color == Card.Black
        end)

        data.extra_data = data.extra_data or {}
        data.extra_data.yisiquanhuangSideCards = {}
        if #redCards > 0 then
            data.extra_data.yisiquanhuangSideCards.red = {redCards[1],redCards[#redCards]}
        end
        if #blackCards > 0 then
            data.extra_data.yisiquanhuangSideCards.black = {blackCards[1],blackCards[#blackCards]}
        end
    end,
})

yisiquanhuang:addEffect(fk.EventLoseSkill, {
    can_refresh = function(self, event, target, player, data)
        return target == player and data.skill:getSkeleton().name == yisiquanhuang.name
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        local handcards = player:getCardIds("h")
        local redCards = table.filter(handcards,function (id)
            local card = Fk:getCardById(id)
            return card.color == Card.Red
        end)
        local blackCards = table.filter(handcards,function (id)
            local card = Fk:getCardById(id)
            return card.color == Card.Black
        end)

        for _, id in ipairs(redCards) do
            local card = Fk:getCardById(id)
            if card:getMark("@@rmt__yisiquanhuang_left-inhand") ~= 0 then
                room:setCardMark(card, "@@rmt__yisiquanhuang_left-inhand", 0)
            end
            if card:getMark("@@rmt__yisiquanhuang_right-inhand") ~= 0 then
                room:setCardMark(card, "@@rmt__yisiquanhuang_right-inhand", 0)
            end
        end
        for _, id in ipairs(blackCards) do
            local card = Fk:getCardById(id)
            if card:getMark("@@rmt__yisiquanhuang_left-inhand") ~= 0 then
                room:setCardMark(card, "@@rmt__yisiquanhuang_left-inhand", 0)
            end
            if card:getMark("@@rmt__yisiquanhuang_right-inhand") ~= 0 then
                room:setCardMark(card, "@@rmt__yisiquanhuang_right-inhand", 0)
            end
        end
        room:setPlayerMark(player, "@[rmt__yisiquanhuang]", 0)
        room:setPlayerMark(player, MarkEnum.SortProhibited, 0)
    end,
})

yisiquanhuang:addEffect(fk.BeforeCardsMove, {
    can_refresh = function (self, event, target, player, data)
        if not player:hasSkill(yisiquanhuang.name, true) or player:isKongcheng() then
            return false
        end
        return player:hasSkill(yisiquanhuang.name)
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        local handcards = player:getCardIds("h")
        local redCards = table.filter(handcards,function (id)
            local card = Fk:getCardById(id)
            return card.color == Card.Red
        end)
        local blackCards = table.filter(handcards,function (id)
            local card = Fk:getCardById(id)
            return card.color == Card.Black
        end)

        for _, move in ipairs(data) do
            if move.from == player then
                for _, info in ipairs(move.moveInfo) do
                    if info.fromArea == Card.PlayerHand then
                        move.extra_data = move.extra_data or {}
                        if table.contains(redCards,info.cardId) then
                            move.extra_data.rmt__yisiquanhuangRed = true
                        elseif table.contains(blackCards,info.cardId) then
                            move.extra_data.rmt__yisiquanhuangBlack = true
                        end
                        if move.extra_data.rmt__yisiquanhuangRed and move.extra_data.rmt__yisiquanhuangBlack then
                            break
                        end
                    end
                end
            end
        end
    end,
})

yisiquanhuang:addEffect(fk.EventAcquireSkill, {
    can_refresh = function (self, event, target, player, data)
        if not player:hasSkill(yisiquanhuang.name, true) or player:isKongcheng() then
            return false
        end
        return target == player and data.skill:getSkeleton().name == yisiquanhuang.name and not player.room:getTag("FirstRound")
    end,
    on_refresh = spec.on_refresh,
})

yisiquanhuang:addEffect(fk.GameStart,{
    can_refresh = function (self, event, target, player, data)
        if not player:hasSkill(yisiquanhuang.name, true) or player:isKongcheng() then
            return false
        end
        return true
    end,
    on_refresh = spec.on_refresh,
})

yisiquanhuang:addAcquireEffect(function (self, player, is_start)
    player.room:setPlayerMark(player, MarkEnum.SortProhibited, 1)
    player.room:setPlayerMark(player, "@[rmt__yisiquanhuang]", { value = player.id })
end)

return yisiquanhuang